r"""识别红色激光点(openmv挂载的激光笔)

*********************************************
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '&lt;  `.___\_&lt;|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='

                    佛祖保佑       电赛国一
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"""
import sensor
import time
import math
import pyb


thresholds = (86, 100, -11, 6, -15, 39)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=200)
sensor.set_auto_gain(False)  # must be turned off for color tracking
sensor.set_auto_whitebal(False)  # must be turned off for color tracking

led = pyb.LED(3)

class Serial(pyb.UART):
    """串口类"""
    def __init__(self):
        """初始化"""

        super().__init__(3,9600)
        self.head = 0xb3     # 包头
        self.head = self.head.to_bytes(1, 'big')
        self.tail = 0x5b     # 包尾
        self.tail = self.tail.to_bytes(1, 'big')

    @staticmethod
    def get_byte_size(n):
        if n == 0:
            return 1
        bytes = 0
        while n:
            n >>= 8
            bytes += 1
        return bytes

    @staticmethod
    def send(data:int, uart:pyb.UART):
        # 将数据转换为字节串
        data_bytes = data.to_bytes(2, 'big')  # 'big'表示大端字节序，第一个是发送的字节大小

        led.on()
        # 通过串口发送数据
        uart.write(data_bytes)
        time.sleep(0.01)
        led.off()
        time.sleep(0.01)

    def send_arr(self, *args):
        self.write(self.head)
        for i in args:
            Serial.send(i, self)
        self.write(self.tail)

if __name__ == '__main__':
    # 初始化串口
    uart = Serial()

    while True:
        img = sensor.snapshot()

        # 旋转图像
        img.rotation_corr(y_rotation=180, x_rotation=180)

        img.lens_corr(1.8)      # 镜头校正

        for blob in img.find_blobs(
            [thresholds],
            pixels_threshold=200,
            area_threshold=200,
            merge=True,
        ):
            # These values depend on the blob not being circular - otherwise they will be shaky.
            if blob.elongation() > 0.5:
                img.draw_edges(blob.min_corners(), color=(255, 0, 0))
                img.draw_line(blob.major_axis_line(), color=(0, 255, 0))
                img.draw_line(blob.minor_axis_line(), color=(0, 0, 255))
            # These values are stable all the time.
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())
            # Note - the blob rotation is unique to 0-180 only.
            img.draw_keypoints(
                [(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20
            )
            print(blob.cx()-150,120-blob.cy())
            uart.send_arr(blob.cx()-150, 120-blob.cy())
            # 中心(150,120)
